/**
 * @file   TcpClient.h
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-05-18
 * @brief  DataBus Client using TCP socket.
 *
 * Copyright (C) 2013  Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LIBDATABUS_CLIENT_TCPCLIENT_H
#define LIBDATABUS_CLIENT_TCPCLIENT_H

#include <DataBus/Client/AbstractClient.h>
#include <QtNetwork/QHostAddress>
#include <QtNetwork/QTcpSocket>

namespace DataBus
{
/**
 * @brief The TcpClient class
 */
class DATABUSSHARED_EXPORT TcpClient : public AbstractClient
{
    Q_OBJECT

public:
    /**
     * @brief Constructor for Client
     *
     * @param parent
     */
    explicit TcpClient(QObject *parent);

    /**
     * @brief Destructor for Client
     */
    ~TcpClient();

    /**
     * @brief Starts the Client
     *
     * @param serverAddress             Server IP address
     * @param serverPort                Server TCP port
     * @param clientId                  Client ID
     * @param clientName                Client Name
     * @param activityTimeout           Activity Timeout
     * @param pingResponseTimeout       Ping Response Timeout
     * @param pingRetryCount            Ping Retry Count
     * @param connectionToServerTimeout Timeout for connecting to the server (time in ms)
     * @param registrationTimeout       Registration timeout (time in ms, min 5000 ms)
     *
     * @retval true     Success
     * @retval false    Error
     */
    bool start(const QHostAddress serverAddress,
               const quint16 serverPort,
               const quint8 clientId,
               const QString &clientName,
               const quint8 activityTimeout,
               const quint8 pingResponseTimeout,
               const quint8 pingRetryCount,
               const int connectionToServerTimeout = 10000,
               const int registrationTimeout = 10000);

    /**
     * @brief Stops the Client
     */
    void stop();

    /**
     * @brief Returns Server Address
     *
     * @return Server Address
     */
    QHostAddress getServerAddress() const;

    /**
     * @brief Returns Server Port
     *
     * @return Server Port
     */
    quint16 getServerPort() const;

private slots:
    /**
     * @brief This slot should be executed when TCP socket was disconnected
     */
    void tcpSocketDisconnected();

    /**
     * @brief This slot should be executed when TCP socket error occurs
     */
    void tcpSocketError(QAbstractSocket::SocketError socketError);

    /**
     * @brief This slot should be executed when there is data available to be read from the socket
     */
    void readData();

private:
    /**
     * @brief Disable TcpClient copy constructor
     */
    TcpClient(const TcpClient &);

    /**
     * @brief Disable TcpClient operator =
     *
     * @return Nothing
     */
    TcpClient &operator=(const TcpClient &);

    /**
     * @brief Sends a DataBus Packet
     *
     * @param packet    DataBus Packet
     *
     * @retval true     Success
     * @retval false    Error
     */
    bool sendPacket(const Packet &packet);

    /**
     * @brief TCP socket
     */
    QTcpSocket m_socket;
};
}

#endif // LIBDATABUS_CLIENT_TCPCLIENT_H
